home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_438 / gadgeted / source / gadgeted.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  15KB  |  514 lines

  1. /*----------------------------------------------------------------------*
  2.   GadgetED.c version 2.0 - © Copyright 1990 Jaba Development
  3.  
  4.   Autor   : Jan van den Baard
  5.   Purpose : main control program.
  6.  *----------------------------------------------------------------------*/
  7.  
  8. USHORT pointer[] =
  9.  { 0x0000,0x0000,0xffff,0xffff,0xc007,0xffff,0xaffb,0xffff,
  10.    0xa439,0xffff,0xd77d,0xffff,0xf6fd,0xffff,0xf43d,0xffff,
  11.    0xf7fd,0xffff,0xf70d,0xffff,0xf7dd,0xffff,0xd7bd,0xffff,
  12.    0xa709,0xffff,0xaffb,0xffff,0xc003,0xffff,0xffff,0xffff,
  13.    0x0000,0x0000,0x0000,0x0000
  14.  };
  15. USHORT Colors[] =
  16.  { 0x0000,0x0ecb,0x0f00,0x0ff0,0x00f0,0x00c0,0x0090,0x0060,
  17.    0x000f,0x000c,0x0009,0x0ff0,0x0cc0,0x0990,0x0f0f,0x0c0c,
  18.    0x0909,0x0a00,0x0000,0x0f00,0x0444,0x0555,0x0666,0x0777,
  19.    0x0888,0x0999,0x0aaa,0x0bbb,0x0ccc,0x0ddd,0x0fff,0x0f00
  20.  };
  21. USHORT info_data[] =
  22.  { 0xee00,0xc600,0xaa00,0xee00,0xee00,0xee00,0xee00,0xaa00,
  23.    0xc600,0xee00,0xfff0,0xef70,0xdfb0,0x8010,0xdfb0,0xef70,
  24.    0xfff0,0xee00,0xc600,0xaa00,0xee00,0xee00,0xee00,0xee00,
  25.    0xee00,0xee00,0xee00,0xfff0,0xeff0,0xdff0,0x8010,0xdff0,
  26.    0xeff0,0xfff0,0xfff0,0xbdf0,0xdbf0,0xe610,0xeff0,0xde10,
  27.    0xbff0,0xfff0,0xfff8,0xbdf8,0xdbf8,0xe708,0xe7f8,0xdb08,
  28.    0xbdf8,0xfff8
  29.  };
  30.  
  31. struct Image HEIGHT_image =
  32.  { 247,0,7,10,1,NULL,0x01,0x00,NULL
  33.  };
  34. struct Image WIDTH_image  =
  35.  { 197,1,12,7,1,NULL,0x01,0x00,&HEIGHT_image
  36.  };
  37. struct Image TOP_image    =
  38.  { 150,0,7,10,1,NULL,0x01,0x00,&WIDTH_image
  39.  };
  40. struct Image LEFT_image   =
  41.  { 100,1,12,7,1,NULL,0x01,0x00,&TOP_image
  42.  };
  43. struct Image YC_image     =
  44.  { 50,1,12,8,1,NULL,0x01,0x00,&LEFT_image
  45.  };
  46. struct Image XC_image     =
  47.  { 0,1,13,8,1,NULL,0x01,0x00,&YC_image
  48.  };
  49.  
  50. #define DATA_SIZE_TOTAL 100
  51. #define HEIGHT_OFFSET   0
  52. #define WIDTH_OFFSET    10
  53. #define TOP_OFFSET      17
  54. #define LEFT_OFFSET     27
  55. #define YC_OFFSET       34
  56. #define XC_OFFSET       42
  57.  
  58. UBYTE wdt[80]       = "Work Window";
  59. UBYTE wlb[MAXLABEL] = "new_window";
  60.  
  61. struct TextAttr std =
  62.  { (STRPTR)"topaz.font",8,FS_NORMAL,FPF_ROMFONT
  63.  };
  64.  
  65. struct NewScreen ns_main =
  66.  { 0,0,640,STDSCREENHEIGHT,2,0,1,HIRES,CUSTOMSCREEN,&std,TITLE,NULL,NULL
  67.  };
  68. struct NewWindow nw_main =
  69.  { 50,25,175,50,0,1,NEWSIZE|GADGETUP|GADGETDOWN|INACTIVEWINDOW|ACTIVEWINDOW|MENUPICK|RAWKEY|MOUSEBUTTONS|CLOSEWINDOW,
  70.    WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE|WINDOWDEPTH|NOCAREREFRESH|SMART_REFRESH|ACTIVATE,NULL,NULL,(UBYTE *)&wdt,NULL,NULL,150,50,
  71.    0,256,CUSTOMSCREEN
  72.  };
  73.  
  74. char name[512];
  75.  
  76. struct GadgetList     Gadgets;
  77. struct Requester      dw;
  78. struct FileRequester *IODir;
  79. struct MemoryChain    Memory;
  80. struct MemoryChain    Misc;
  81. struct ge_prefs       prefs  = { TRUE,TRUE,TRUE };
  82. struct Window        *sysreq;
  83. struct Window        *MainWindow = NULL;
  84. struct Screen        *MainScreen = NULL;
  85. struct RastPort      *MainRP;
  86. struct Gadget        *Gadget;
  87. struct Gadget        TextGadget = { NULL,0,0,1,1,GADGHNONE,NULL,BOOLGADGET,
  88.                                     NULL,NULL,NULL,NULL,NULL,NULL,NULL };
  89.  
  90. struct IntuitionBase *IntuitionBase;
  91. struct GfxBase       *GfxBase;
  92. struct ToolBase      *ToolBase;
  93.  
  94. BYTE    Xoff = -1, Yoff = 0;
  95. BOOL    Saved = TRUE;
  96. BOOL    REQUESTER = FALSE;
  97. BOOL    WORKBENCH = FALSE;
  98. BOOL    WBSCREEN  = FALSE;
  99. ULONG   WindowFlags = WINDOWSIZING+WINDOWDRAG+WINDOWCLOSE+WINDOWDEPTH+NOCAREREFRESH+SMART_REFRESH+ACTIVATE;
  100. ULONG   IDCMPFlags  = GADGETUP+GADGETDOWN+CLOSEWINDOW;
  101. ULONG   Class;
  102. USHORT  BackFill  = 1;
  103. USHORT  Code, Qualifier;
  104. USHORT *pd, *data_pointer;
  105. USHORT  FrontPen = 1, BackPen = 0, GadgetCount = 0, id = 0, DEPTH;
  106. SHORT   MainX, MainY;
  107.  
  108. extern struct MenuItem   SubItems[];
  109. extern struct Menu       Titles[];
  110. extern struct WBStartup *WBenchMsg;
  111.  
  112. /*
  113.  * free and close all resources
  114.  */
  115. VOID close_up(msg)
  116.  UBYTE *msg;
  117. {
  118.      struct Process *proc;
  119.  
  120.      FreeGList();
  121.      FreeFreq(IODir);
  122.      proc = (struct Process *)FindTask(NULL);
  123.      proc->pr_WindowPtr = (APTR)sysreq;
  124.      if(MainWindow)  { ClearMenuStrip(MainWindow); CloseWindow(MainWindow); }
  125.      if(MainScreen)    CloseScreen(MainScreen);
  126.                        OpenWorkBench();
  127.      if(msg)           puts(msg);
  128.                        FreeMemoryChain(&Misc);
  129.      if(ToolBase)      CloseLibrary(ToolBase);
  130.      exit(0);
  131. }
  132.  
  133. /*
  134.  * allocate open and setup all resources
  135.  */
  136. VOID open_libs()
  137. {
  138.      if(NOT(ToolBase = (struct ToolBase *)
  139.       OpenLibrary("tool.library",TOOL_VERSION)))
  140.         close_up("   - ERROR: Can't find the tool.library V7++!");
  141.  
  142.      GfxBase       = ToolBase->GfxBase;
  143.      IntuitionBase = ToolBase->IntuitionBase;
  144.  
  145.      if(NOT(IODir = AllocFreq()))
  146.          close_up("   - ERROR: Out of memory !");
  147.  
  148.      InitMemoryChain(&Misc,1024L);
  149.      InitMemoryChain(&Memory,(5*1024));
  150.  
  151.      if(NOT(pd = (USHORT *)
  152.       AllocItem(&Misc,(POINTERSIZE << 1),MEMF_CHIP+MEMF_CLEAR)))
  153.          close_up("   - ERROR: Out of memory !");
  154.      if(NOT(data_pointer = (USHORT *)
  155.       AllocItem(&Misc,DATA_SIZE_TOTAL,MEMF_CHIP+MEMF_CLEAR)))
  156.          close_up("   ERROR: Out of memory !");
  157.  
  158.      CopyMem((void *)&pointer,(void *)pd,(POINTERSIZE << 1));
  159.      CopyMem((void *)&info_data,(void *)data_pointer,DATA_SIZE_TOTAL);
  160.  
  161.      HEIGHT_image.ImageData = (USHORT *)&data_pointer[HEIGHT_OFFSET];
  162.      WIDTH_image.ImageData  = (USHORT *)&data_pointer[WIDTH_OFFSET];
  163.      TOP_image.ImageData    = (USHORT *)&data_pointer[TOP_OFFSET];
  164.      LEFT_image.ImageData   = (USHORT *)&data_pointer[LEFT_OFFSET];
  165.      YC_image.ImageData     = (USHORT *)&data_pointer[YC_OFFSET];
  166.      XC_image.ImageData     = (USHORT *)&data_pointer[XC_OFFSET];
  167. }
  168.  
  169. /*
  170.  * open up the main display
  171.  */
  172. VOID open_display()
  173. {
  174.      if(NOT MainScreen)
  175.      {   if(NOT(MainScreen = OpenScreen(&ns_main)))
  176.              close_up("   - ERROR: Can't open a work screen !");
  177.      }
  178.      nw_main.Screen    = MainScreen;
  179.      nw_main.MaxHeight = MainScreen->Height;
  180.      if(NOT(MainWindow = OpenWindow(&nw_main)))
  181.          close_up("   - ERROR: Can't open a work window !");
  182.      MainRP = MainWindow->RPort;
  183. }
  184.  
  185. /*
  186.  * disable the edit window by putting up a little requester
  187.  */
  188. VOID disable_window()
  189. {
  190.     InitRequester(&dw);
  191.     dw.LeftEdge = 0;
  192.     dw.TopEdge  = 0;
  193.     dw.Width    = 1;
  194.     dw.Height   = 1;
  195.     dw.BackFill = 1;
  196.     Request(&dw,MainWindow);
  197. }
  198.  
  199. /*
  200.  * enable the edit window (remove the requester)
  201.  */
  202. VOID enable_window()
  203. {
  204.     EndRequest(&dw,MainWindow);
  205. }
  206.  
  207. /*
  208.  * show buisy pointer
  209.  */
  210. VOID buisy()
  211. {
  212.     SetPointer(MainWindow,(void *)pd,16,16,Xoff,Yoff);
  213. }
  214.  
  215. /*
  216.  * erase buisy pointer
  217.  */
  218. VOID ok()
  219. {
  220.     ClearPointer(MainWindow);
  221. }
  222.  
  223. /*
  224.  * change the depth of the edit screen
  225.  */
  226. VOID change_depth(depth)
  227.     ULONG depth;
  228. {
  229.     if(MainWindow)
  230.     {   ClearMenuStrip(MainWindow);
  231.         CloseWindow(MainWindow);
  232.     }
  233.     if(MainScreen)
  234.     {   if(depth != MainScreen->BitMap.Depth)
  235.         {   CloseScreen(MainScreen);
  236.             MainScreen = NULL;
  237.         }
  238.     }
  239.     ns_main.Depth = depth;
  240.  
  241.     if(depth == 5)
  242.     {   ns_main.ViewModes = NULL;
  243.         ns_main.Width     = 320;
  244.         if(NOT nw_main.MaxWidth) nw_main.MaxWidth  = 320;
  245.     }
  246.     else
  247.     {   ns_main.ViewModes = HIRES;
  248.         ns_main.Width     = 640;
  249.         if(NOT nw_main.MaxWidth) nw_main.MaxWidth  = 640;
  250.     }
  251.     if(REQUESTER) nw_main.Flags |= (GIMMEZEROZERO+SIZEBRIGHT+SIZEBBOTTOM);
  252.     open_display();
  253.     if(REQUESTER)
  254.     {   SetDrMd(MainRP,JAM1);
  255.         SetAPen(MainRP,BackFill);
  256.         BackPen = 1;
  257.         FrontPen = 0;
  258.         RectFill(MainRP,0,0,MainWindow->GZZWidth,MainWindow->GZZHeight);
  259.     }
  260.     SetMenu(MainWindow);
  261. }
  262.  
  263. /*
  264.  * load the preferences file (if available)
  265.  */
  266. VOID load_prefs()
  267. {
  268.     BPTR file;
  269.  
  270.     if((file = Open("DEVS:GadgetEd.PREFS",MODE_OLDFILE)))
  271.     {   if(Read(file,(char *)&prefs,sizeof(struct ge_prefs)) <= 0)
  272.         {   Close(file);
  273.             Error("Error reading preferences !");
  274.             return;
  275.         }
  276.         Close(file);
  277.     }
  278. }
  279.  
  280. /*
  281.  * if a gadget has GRELWITH or GRELHEIGHT set and it's
  282.  * a BOOL or STRGADGET gadget with a border this routine
  283.  * sizes the border to fit around the gadget again after
  284.  * a resizing of the window.
  285.  */
  286. VOID grl()
  287. {
  288.     register struct Gadget *g, *g1;
  289.     register SHORT *XY;
  290.  
  291.     if(Gadgets.TailPred == (struct MyGadget *)&Gadgets) return;
  292.  
  293.     g = &Gadgets.TailPred->Gadget;
  294.     disable_window();
  295.     while(1)
  296.     {   g1 = g->NextGadget;
  297.         un_grel(MainWindow,g);
  298.         if((TestBits((ULONG)g->Flags,GRELWIDTH)) AND
  299.            (NOT TestBits((ULONG)g->Flags,GADGIMAGE)))
  300.         {   if(NOT TestBits((ULONG)g->GadgetType,PROPGADGET))
  301.             {   XY = ((struct Border *)g->GadgetRender)->XY;
  302.                 if(g->Width < 9)
  303.                 {   XY[2] = XY[4] = 9;
  304.                     g->Width = 9;
  305.                 }
  306.                 else
  307.                 { XY[2] = XY[4] = g->Width;
  308.                 }
  309.             }
  310.         }
  311.         if((TestBits((ULONG)g->Flags,GRELHEIGHT)) AND
  312.            (NOT TestBits((ULONG)g->Flags,GADGIMAGE)))
  313.         {   if(NOT TestBits((ULONG)g->GadgetType,PROPGADGET))
  314.             {   XY = ((struct Border *)g->GadgetRender)->XY;
  315.                 if(g->Height < 9)
  316.                 {   XY[5] = XY[7] = 9;
  317.                     g->Height = 9;
  318.                 }
  319.                 else
  320.                 { XY[5] = XY[7] = g->Height;
  321.                 }
  322.             }
  323.         }
  324.         grel(MainWindow,g);
  325.         if(NOT g1) break;
  326.         g = g1;
  327.     }
  328.     enable_window();
  329. }
  330.  
  331. /*
  332.  * set screen title. This does not use SetWindowTitles() because if
  333.  * the edit window overlaps the screen title the message must still
  334.  * be displayed.
  335.  */
  336. VOID sst(title)
  337.     char *title;
  338. {
  339.     struct RastPort *rp;
  340.  
  341.     rp = &MainScreen->RastPort;
  342.     SetAPen(rp,1);
  343.     RectFill(rp,0,0,MainScreen->Width,9); /* clear the title bar */
  344.     if(title)
  345.     {   SetAPen(rp,0);
  346.         SetBPen(rp,1);
  347.         SetDrMd(rp,JAM2);
  348.         Move(rp,4,7);
  349.         Text(rp,title,strlen(title));
  350.     }
  351. }
  352.  
  353. /*
  354.  * setup the info display
  355.  */
  356. VOID set_info()
  357. {
  358.     struct RastPort *rp;
  359.  
  360.     rp = &MainScreen->RastPort;
  361.     sst(NULL);
  362.     DrawImage(rp,&XC_image,0,0);
  363. }
  364.  
  365. /*
  366.  * update the info line
  367.  */
  368. VOID do_info(x,y,x1,y1)
  369.     register SHORT x,y,x1,y1;
  370. {
  371.     char  mx[5],my[5],l[5],t[5],w[5],h[5];
  372.     SHORT xx,yy,tmp;
  373.     struct RastPort *rp;
  374.  
  375.     rp = &MainScreen->RastPort;
  376.  
  377.     get_xy(&xx,&yy);
  378.     Format((char *)&mx,"%-4ld",xx);
  379.     Format((char *)&my,"%-4ld",yy);
  380.  
  381.     if(x1 < x) { tmp = x; x = x1; x1 = tmp; }
  382.     if(y1 < y) { tmp = y; y = y1; y1 = tmp; }
  383.  
  384.     Format((char *)&l,"%-4ld",x);
  385.     Format((char *)&t,"%-4ld",y);
  386.     Format((char *)&w,"%-3ld",x1-x);
  387.     Format((char *)&h,"%-3ld",y1-y);
  388.  
  389.     SetAPen(rp,0);
  390.     SetBPen(rp,1);
  391.     SetDrMd(rp,JAM2);
  392.     Move(rp,15,7);
  393.     Text(rp,(char *)&mx,4);
  394.     Move(rp,64,7);
  395.     Text(rp,(char *)&my,4);
  396.     Move(rp,114,7);
  397.     Text(rp,(char *)&l,4);
  398.     Move(rp,159,7);
  399.     Text(rp,(char *)&t,4);
  400.     Move(rp,211,7);
  401.     Text(rp,(char *)&w,3);
  402.     Move(rp,256,7);
  403.     Text(rp,(char *)&h,3);
  404. }
  405.  
  406. /*
  407.  * entry point
  408.  */
  409. VOID main(argc,argv)
  410.     ULONG argc;
  411.     char *argv[];
  412. {
  413.     SHORT x,y,x1,y1;
  414.     BOOL running = TRUE;
  415.     struct WBArg *wba;
  416.     struct Process *proc;
  417.  
  418.     proc = (struct Process *)FindTask(NULL);
  419.     sysreq = (struct Window *)proc->pr_WindowPtr;
  420.     proc->pr_WindowPtr = (APTR)-1L;
  421.  
  422.     open_libs();
  423.     NewList((void *)&Gadgets);
  424.  
  425.     if(NOT argc)
  426.     {   if(WBenchMsg->sm_NumArgs > 1)
  427.         {   wba = WBenchMsg->sm_ArgList;
  428.             wba++;
  429.             strcpy((char *)&name[0],(char *)wba->wa_Name);
  430.             ReadBinGadgets(TRUE);
  431.         }
  432.         else
  433.         {   get_config();
  434.             change_depth(DEPTH);
  435.             if(NOT WBSCREEN) LoadRGB4(&MainScreen->ViewPort,(void *)&Colors,(1 << DEPTH));
  436.             load_prefs();
  437.         }
  438.     }
  439.     else if(argc == 1)
  440.     {   get_config();
  441.         change_depth(DEPTH);
  442.         if(NOT WBSCREEN) LoadRGB4(&MainScreen->ViewPort,(void *)&Colors,(1 << DEPTH));
  443.         load_prefs();
  444.     }
  445.     else if(argc > 2) close_up("   - USAGE: GadgetEd [name]");
  446.     else
  447.     {   strcpy((char *)&name[0],argv[1]);
  448.         ReadBinGadgets(TRUE);
  449.     }
  450.  
  451.     do
  452.     {   SetWindowTitles(MainWindow,(char *)&wdt[0],(char *)TITLE);
  453.         Wait(1 << MainWindow->UserPort->mp_SigBit);
  454.         while(read_msg(MainWindow))
  455.         {   switch(Class)
  456.             {   case GADGETUP:
  457.                 case GADGETDOWN:     break;
  458.  
  459.                 case ACTIVEWINDOW:   while(read_msg(MainWindow));
  460.                                      break;
  461.  
  462.                 case INACTIVEWINDOW: if(NOT IntuitionBase->ActiveWindow)
  463.                                          ActivateWindow(MainWindow);
  464.                                      break;
  465.  
  466.                 case NEWSIZE:        grl();
  467.                                      refresh();
  468.                                      Saved = FALSE;
  469.                                      break;
  470.  
  471.                 case RAWKEY:         handle_keys(Code,Qualifier);
  472.                                      while(read_msg(MainWindow));
  473.                                      break;
  474.  
  475.                 case MENUPICK:       handle_menus(Code);
  476.                                      break;
  477.  
  478.                 case MOUSEBUTTONS:   if(Code == SELECTDOWN)
  479.                                      {   set_info();
  480.                                          get_xy(&x,&y);
  481.                                          MainX = x;
  482.                                          MainY = y;
  483.                                          SetAPen(MainRP,FrontPen);
  484.                                          SetDrMd(MainRP,COMPLEMENT);
  485.                                          draw_box(MainWindow,MainX,MainY,x,y);
  486.                                          do_info(MainX,MainY,x,y);
  487.                                          while(Code == SELECTDOWN)
  488.                                          {   while(read_msg(MainWindow));
  489.                                          }
  490.                                          while(Code != SELECTDOWN)
  491.                                          {   while(read_msg(MainWindow))
  492.                                              {   if(Class == MENUPICK) set_info();
  493.                                                  do_info(MainX,MainY,x,y);
  494.                                              }
  495.                                              get_xy(&x1,&y1);
  496.                                              if((x1 != x) OR (y1 != y))
  497.                                              {   draw_box(MainWindow,MainX,MainY,x,y);
  498.                                                  get_xy(&x,&y);
  499.                                                  draw_box(MainWindow,MainX,MainY,x,y);
  500.                                                  do_info(MainX,MainY,x,y);
  501.                                              }
  502.                                          }
  503.                                          add_gadget(MainWindow,MainX,MainY,x,y);
  504.                                          refresh();
  505.                                      }
  506.                                      break;
  507.  
  508.                 default:             break;
  509.             }
  510.         }
  511.     } while(running == TRUE);
  512.     close_up("  - BYE BYE");
  513. }
  514.